//
//  UseTimeDetailInfoCell.swift
//  EasePods
//
//  Created by 李然 on 2024/5/30.
//

import UIKit

class UseTimeDetailInfoCell: TableViewCell {
    
    override func makeUI() {
        super.makeUI()
        
        contentView.addSubview(bgView)
        bgView.snp.makeConstraints { make in
            make.edges.equalTo(UIEdgeInsets(top: 0, left: 0, bottom: 20, right: 0))
        }
        
        bgView.addArrangedSubview(mediaUseTimeView)
        bgView.addArrangedSubview(callUseTimeView)
        bgView.addArrangedSubview(waitUseTimeView)
        bgView.addArrangedSubview(timesView)
        timesView.lineView.isHidden = true
    }
    
    func setData(_ data: UseTimeDetailAnalyzeData) {
        self.mediaUseTimeView.detailLabel.text = data.mediaTimeMin.hourAndMinTimeFormat
        self.callUseTimeView.detailLabel.text = data.callTimeMin.hourAndMinTimeFormat
        self.waitUseTimeView.detailLabel.text = data.standbyTimeMin.hourAndMinTimeFormat
        
        if let times = data.frequency {
            self.timesView.detailLabel.text = "\(times)" + R.string.localizable.common_unit_times()
        } else {
            self.timesView.detailLabel.text = "--" + R.string.localizable.common_unit_times()
        }
    }
    
    lazy var bgView: StackView = {
        let view = StackView()
        view.axis = .vertical
        view.alignment = .center
        view.distribution = .fill
        view.backgroundColor = R.color.use_time_18_bg_main()
        return view
    }()

    private lazy var mediaUseTimeView: InfoView = {
        let view = InfoView()
        view.titleLabel.text = R.string.localizable.use_time_media_time()
        view.iconImageView.backgroundColor = R.color.use_time_3_media()
        view.innerImageView.image = R.image.ic_use_time_media()
        view.iconImageView.layer.cornerRadius = 9
        return view
    }()

    private lazy var callUseTimeView: InfoView = {
        let view = InfoView()
        view.titleLabel.text = R.string.localizable.use_time_calling_time()
        view.iconImageView.backgroundColor = R.color.use_time_2_phone()
        view.innerImageView.image = R.image.ic_use_time_phone()
        view.iconImageView.layer.cornerRadius = 9
        return view
    }()
    
    private lazy var waitUseTimeView: InfoView = {
        let view = InfoView()
        view.titleLabel.text = R.string.localizable.use_time_waiting_time()
        view.iconImageView.backgroundColor = R.color.use_time_4_unused()
        view.innerImageView.image = R.image.ic_use_time_unuse()
        view.iconImageView.layer.cornerRadius = 9
        return view
    }()
    
    private lazy var timesView: InfoView = {
        let view = InfoView()
        view.titleLabel.text = R.string.localizable.use_time_frequency()
        view.iconImageView.image = R.image.ic_use_time_times()
        return view
    }()
}


private class InfoView: StackView {
    
    override func makeUI() {
        super.makeUI()
        
        backgroundColor = R.color.use_time_18_bg_main()
        
        axis = .horizontal
        alignment = .center
        distribution = .fill
        spacing = 7
        
        snp.makeConstraints { make in
            make.width.equalTo(kScreenWidth - 40)
            make.height.greaterThanOrEqualTo(70)
        }
        
        addArrangedSubview(iconImageView)
        addArrangedSubview(titleLabel)
        let spaceLabel = Label()
        spaceLabel.text = " "
        spaceLabel.setPriority(.fittingSizeLevel, for: .horizontal)
        addArrangedSubview(spaceLabel)
        addArrangedSubview(detailLabel)
        
        addSubview(lineView)
        lineView.snp.makeConstraints { make in
            make.centerX.bottom.equalToSuperview()
            make.size.equalTo(CGSize(width: kScreenWidth - 40, height: 0.5))
        }
    }
    
    lazy var lineView: UIView = {
        let line = View()
        line.backgroundColor = R.color.use_time_21_line_item()
        return line
    }()
    
    lazy var iconImageView: UIImageView = {
        let view = UIImageView()
        view.addSubview(innerImageView)
        innerImageView.snp.makeConstraints { make in
            make.center.equalToSuperview()
        }
        view.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: 18, height: 18))
        }
        return view
    }()
    
    lazy var innerImageView: UIImageView = {
        let view = UIImageView()
        return view
    }()
    
    lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.textColor = R.color.use_time_19_text_item()
        label.font = Fonts.systemFont(ofSize: 14)
        label.numberOfLines = 0
        return label
    }()
    
    lazy var detailLabel: UILabel = {
        let label = UILabel()
        label.textColor = R.color.use_time_19_text_item()
        label.font = Fonts.systemFont(ofSize: 14)
        label.numberOfLines = 0
        return label
    }()
}
